package com.jeeplus.modules.basicdata.standard.mapper;

import com.jeeplus.common.utils.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConvertArrayTypeHandler extends BaseTypeHandler<String[]> {
    public static final String SEPARATOR = ",";

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String[] strings, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, StringUtils.join(strings, ConvertArrayTypeHandler.SEPARATOR));
    }

    @Override
    public String[] getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return resultSet.getString(s).split(SEPARATOR);
    }

    @Override
    public String[] getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return resultSet.getString(i).split(SEPARATOR);
    }

    @Override
    public String[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return callableStatement.getString(i).split(SEPARATOR);
    }
}
